/**
 * 
 */
package swt.action;

import java.io.IOException;
import java.util.TimerTask;
import java.util.logging.Logger;

import swt.commons.LogUtil;

/**
 * @author Thuc
 *
 */
public class Task extends TimerTask {

	private static Logger log = LogUtil.getLogger();
	
	private long milisec;

	/**
	 * Constructor
	 * @param milisec
	 */
	public Task(long milisec){
		this.milisec = milisec;
		
		long n = (int) ((this.milisec - System.currentTimeMillis()) / (1000L));
		log.info("remaining seconds:" + String.valueOf(n));
	}
	
	/**
	 * Main process
	 */
	public void run() {

		long n = (int) ((this.milisec - System.currentTimeMillis()) / (1000L));
		
		if (n <= 0) {
			try {
				log.info("Time is up!!!");
				// Cancel task
				this.cancel();
				
				// Shut down command: 
				// shutdown /i /p /f /d P:7:0
				ProcessBuilder processBuilder = new ProcessBuilder(new String[]{"shutdown", "/p", "/f", "/d", "P:7:0"});
				try {
					processBuilder.start();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
				
			} catch (java.lang.RuntimeException ex) {
				throw ex;
			}
		}//end of if
	}
}
